package com.liliudong.keelflower.upms.admin.controller.sys;

import com.liliudong.keelflower.common.core.constants.Const;
import com.liliudong.keelflower.common.core.utils.R;
import com.liliudong.keelflower.common.core.utils.validation.Insert;
import com.liliudong.keelflower.upms.admin.controller.BaseController;
import com.liliudong.keelflower.upms.biz.entity.sys.SysMenu;
import com.liliudong.keelflower.upms.biz.entity.sys.SysOrg;
import com.liliudong.keelflower.upms.biz.entity.sys.SysUser;
import com.liliudong.keelflower.upms.biz.service.sys.SysMenuService;
import com.liliudong.keelflower.upms.biz.service.sys.SysOrgService;
import com.liliudong.keelflower.upms.biz.service.sys.SysRoleService;
import com.liliudong.keelflower.upms.biz.service.sys.SysUserService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author liudong.li
 * @since 2021-06-07
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("/upms.admin/sys-user")
public class SysUserController extends BaseController<SysUserService, SysUser> {

    private final SysRoleService sysRoleService;
    private final SysMenuService sysMenuService;
    private final SysOrgService sysOrgService;

    public SysUserController(SysUserService service,
                             SysRoleService sysRoleService,
                             SysMenuService sysMenuService,
                             SysOrgService sysOrgService) {
        super(service);
        this.sysRoleService = sysRoleService;
        this.sysMenuService = sysMenuService;
        this.sysOrgService = sysOrgService;
    }

    /**
     * 用户登录
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/login")
    public R<?> login(@RequestBody @Validated({Insert.class}) SysUser sysUser) {
        String username = sysUser.getUsername();
        String password = sysUser.getPassword();
        String token = service.login(username, password);
        return R.success(token);
    }

    /**
     * 退出登录
     *
     * @param token
     * @return
     */
    @RequiresUser
    @GetMapping("/logout")
    public R<?> logout(@RequestHeader(value = Const.DEFAULT_HEADER) String token) {
        service.logout(token);
        return R.success();
    }

    /**
     * 获取登录用户的基本信息
     *
     * @return
     */
    @RequiresUser
    @GetMapping("/info")
    public R<?> info() {
        SysUser loginUser = getLoginUser();
        Set<String> roleCodes = sysRoleService.getRoleCodesByUserId(loginUser.getId());
        List<SysMenu> menuList = sysMenuService.getUserMenusByUserId(loginUser.getId(), loginUser.isSuperAdmin());
        List<SysOrg> orgList = sysOrgService.getListByUserId(loginUser.getId(), loginUser.isSuperAdmin());
        loginUser.setRoles(roleCodes);
        loginUser.setSysMenuList(menuList);
        loginUser.setSysOrgList(orgList);
        return R.success(loginUser);
    }
}

